using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
using Transport;

namespace SendKeys
{
    public partial class SendKeysToApps : Form
    {
        private bool isStartServer;
        private delegate void UpdateStatusCallback(string strMessage);
        private Server server;

        /// <summary>
        /// Default constructor
        /// </summary>
        public SendKeysToApps()
        {
            InitializeComponent();
            RefreshWindows();
        }
        

        /// <summary>
        /// Closing the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDone_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Refresh the combobox list with all the top level windows running on desktop.
        /// </summary>
        private void RefreshWindows()
        {
            cboWindows.Items.Clear();
            GetTaskWindows();
        }
        
        /// <summary>
        /// Allows combobox and textbox switching on selection of Auto and Manual.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OptionSelection(object sender, EventArgs e)
        {
            if (rbAuto.Checked == true)
            {
                cboWindows.Visible = true;
                txtTitle.Text = cboWindows.Text;
                txtTitle.Visible = false;
            }
            else
            {
                cboWindows.Visible = false;
                txtTitle.Text = cboWindows.Text;
                txtTitle.Visible = true;
            }
        }

       
        private void lnkRefresh_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RefreshWindows();
        }
        /// <summary>
        /// Get all the top level visible windows
        /// </summary>
        private void GetTaskWindows()
        {
            // Get the desktopwindow handle
            int nDeshWndHandle = NativeWin32.GetDesktopWindow();
            // Get the first child window
            int nChildHandle = NativeWin32.GetWindow(nDeshWndHandle, NativeWin32.GW_CHILD);
                    	
            while (nChildHandle != 0)
	        {
                //If the child window is this (SendKeys) application then ignore it.
		        if (nChildHandle == this.Handle.ToInt32())
                {
                    nChildHandle = NativeWin32.GetWindow(nChildHandle, NativeWin32.GW_HWNDNEXT);
                }

                // Get only visible windows
                if (NativeWin32.IsWindowVisible(nChildHandle) != 0)
                {
                    StringBuilder sbTitle = new StringBuilder(1024);
                    // Read the Title bar text on the windows to put in combobox
                    NativeWin32.GetWindowText(nChildHandle, sbTitle, sbTitle.Capacity);
                    String sWinTitle = sbTitle.ToString();
                    {
                        if (sWinTitle.Length > 0)
                        {
                            cboWindows.Items.Add(sWinTitle);
                        }
                    }
                }
                // Look for the next child.
                nChildHandle = NativeWin32.GetWindow(nChildHandle, NativeWin32.GW_HWNDNEXT);
	        }
        }

        

        private void btnListen_Click(object sender, EventArgs e)
        {
            if (!isStartServer)
            {
                Server.StatusChanged += ServerStatusChanged;
                server = new Server(IPAddress.Parse(boxIp.Text), 9595);
                server.StartListening();
                txtLog.AppendText("Monitoring for connections...\r\n");
            }
            isStartServer = true;
        }


        public void ServerStatusChanged(object sender, StatusChangedEventArgs e)
        {
            // Call the method that updates the form
            Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
        }

        private void UpdateStatus(string strMessage)
        {
            // Updates the log with the message
            txtLog.AppendText(strMessage + "\r\n");
            int iHandle = NativeWin32.FindWindow(null, txtTitle.Text);
            NativeWin32.SetForegroundWindow(iHandle);
            System.Windows.Forms.SendKeys.Send(strMessage);
        }

        private void btnSendKeys_Click(object sender, EventArgs e)
        {

        }


        
    }
}